package com.ljkj.blockpay.ui.fragment;

import static android.app.Activity.RESULT_OK;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseFragment;
import com.ljkj.blockpay.basedata.BuildConfig;
import com.ljkj.blockpay.databinding.FragmentAssetBinding;
import com.ljkj.blockpay.presenter.AssetPresenter;
import com.ljkj.blockpay.presenter.inter.AssetPresenterInter;
import com.ljkj.blockpay.ui.activity.LoginActivity;
import com.ljkj.blockpay.ui.activity.ScanActivity;
import com.ljkj.blockpay.ui.adapter.HomeCurrencyAdapter;
import com.ljkj.blockpay.util.KeyboardUtil;
import com.ljkj.blockpay.util.StatusBarUtil;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.luck.picture.lib.tools.ToastUtils;
import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.yxing.ScanCodeConfig;
import com.yxing.def.ScanMode;
import com.yxing.def.ScanStyle;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class AssetFragment extends BaseFragment<FragmentAssetBinding, AssetPresenter>  implements AssetPresenterInter.AssetPresenterInterview, ClickListener {

    private AssetFragment assetFragment;
    private HomeCurrencyAdapter mHomeCurrencyAdapter;
    private String totalBalance;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setRootViewFitsSystemWindows(getActivity(),true);
    }
    @Override
    protected int initContentView() {
        return R.layout.fragment_asset;
    }

    @Override
    protected AssetPresenter createPresenter() {
        return new AssetPresenter(getActivity(),this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding.setOnClick(this::onClick);
        KeyboardUtil.hideKeyboard(getActivity());
    }

    @Override
    protected void initData() {
        super.initData();
        //xichengcheng@lianjingkeji.com
        //Admin123456%
        //就这个接口
        getPresenter().balanceInfo();

        getPresenter().banner();
        if (BuildConfig.getToken().equals("")){
            binding.tvHomelogin.setText("点击注册/登录查看");
        } else {

            binding.tvYue.setText("总余额折合UDST");
            binding.tvHomelogin.setText(totalBalance);
            binding.tbnEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (compoundButton.isChecked()&&b == true){
                        binding.tvHomelogin.setText(totalBalance);
                    } else {

                        binding.tvHomelogin.setText("******");
                    }
                }
            });
        }
        binding.rvAssetlist.setLayoutManager(new LinearLayoutManager(getActivity()));
        mHomeCurrencyAdapter = new HomeCurrencyAdapter(R.layout.currency_layout);
        binding.rvAssetlist.setAdapter(mHomeCurrencyAdapter);
        mHomeCurrencyAdapter.setOnItemClickListener( (adapter, view, position) -> {
          JsonList  currencyList = (JsonList) mHomeCurrencyAdapter.getData();
            for (int i = 0; i < currencyList.size(); i++) {
                if (!BuildConfig.getToken().isEmpty()){
                    ToggleToActivity.toCurrencyDetailActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }
                }
        });
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_homerecharge:
                if (!BuildConfig.getToken().equals("")){
                    ToggleToActivity.toRechargeActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }

            break;
            case R.id.tv_homelogin:
                if (!BuildConfig.getToken().isEmpty()){
                    ToggleToActivity.toCurrencyDetailActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }


                break;
            case R.id.iv_homecash:
                if (!BuildConfig.getToken().equals("")){
                    ToggleToActivity.toCashActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }

            break;

            case R.id.ll_scan:
                new RxPermissions(this)
                        .requestEachCombined(Manifest.permission.CAMERA)
                        .subscribe(new Observer<Permission>() {
                            @Override
                            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                            }
                            @Override
                            public void onNext(@io.reactivex.rxjava3.annotations.NonNull Permission permission) {
                                if (permission.granted) {
                                    ScanCodeConfig.create(mActivity,assetFragment)
                                            //设置扫码页样式 ScanStyle.NONE：无  ScanStyle.QQ ：仿QQ样式   ScanStyle.WECHAT ：仿微信样式
                                            .setStyle(ScanStyle.CUSTOMIZE)
                                            .setLimitRect(true)
                                            //设置扫码框位置 scanSize : 扫码框大小   offsetX ： x轴偏移量    offsetY ：y轴偏移量   单位 /px
                                            .setScanSize(800, 0, -200)
                                            //是否显示边框上四个角标 true ： 显示  false ： 不显示
                                            .setShowFrame(true)
                                            //设置边框上四个角标颜色
                                            .setFrameColor(R.color.blue_save)
                                            //设置边框上四个角标圆角  单位 /dp
                                            .setFrameRaduis(2)
                                            //设置边框上四个角宽度 单位 /dp
                                            .setFrameWith(4)
                                            //设置边框上四个角长度 单位 /dp
                                            .setFrameLenth(15)
                                            //设置是否显示边框外部阴影 true ： 显示  false ： 不显示
                                            .setShowShadow(true)
                                            //设置边框外部阴影颜色
                                            .setShaowColor(R.color.black_tran30)
                                            //设置扫码条运动方式   ScanMode.REVERSE : 往复运动   ScanMode.RESTART ：重复运动    默认ScanMode.RESTART
                                            .setScanMode(ScanMode.REVERSE)
                                            //设置扫码条扫一次时间  单位/ms  默认3000
                                            .setScanDuration(3000)
                                            //设置扫码条图片
                                            .setScanBitmapId(R.mipmap.scan_line)
                                            .buidler()
                                            //跳转扫码页   扫码页可自定义样式
                                            .start(ScanActivity.class);
                                }
                            }

                            @Override
                            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
//                PermissionX.init(this)
//                        .permissions(Manifest.permission.CAMERA)
//                        .request((allGranted, grantedList, deniedList) -> {
//                            if (allGranted){
////                                ToggleToActivity.toScanActivity(getActivity());
//                                ScanCodeConfig.create(getActivity())
//                                        .setStyle(ScanStyle.NONE)
//                                        .setPlayAudio(true)
//                                        .buidler()
//                                        .start(ScanActivity.class);
//                            } else {
//                                Toast.makeText(getActivity(),"yunx",Toast.LENGTH_LONG);
//                            }
//                        });
                break;
            case R.id.ll_receipt:
                if (!BuildConfig.getToken().equals("")){
                    ToggleToActivity.toReceiptActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }

                break;
            case R.id.ll_exchange:
                if (!BuildConfig.getToken().equals("")){
                    ToggleToActivity.toExchangeActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }

                break;
            case R.id.ll_transfer:
                if (!BuildConfig.getToken().equals("")){
                    ToggleToActivity.toTransferActivity(getActivity());
                } else {
                    ToggleToActivity.toLoginActivity(getActivity());
                }

                break;

            case R.id.iv_news:
            ToggleToActivity.toHistoryTradingActivity(getActivity());
                break;
            default:

                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d("zdl","Fragment====onActivityResult=");

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    @Override
    public void getbalanceInfo(JsonMap jsonMap) {
        totalBalance = jsonMap.getString("totalBalance");
        if (!BuildConfig.getToken().equals("")){
            binding.tvHomelogin.setText(totalBalance);
        }
       JsonList currencyList = jsonMap.getList("currencyList");
        mHomeCurrencyAdapter.setNewData(currencyList);
    }

    @Override
    public void banner(JsonList jsonList) {
        List<JsonMap> list1 = new ArrayList<>();
        List<JsonMap> list2 = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            if (jsonList.getJsonMap(i).getInt("loginFlag") == 1){
                list1.add(jsonList.getJsonMap(i));
            }else{
                list2.add(jsonList.getJsonMap(i));
            }
        }
//        binding.homeBanner.setAdapter(mHomeCurrencyAdapter)
//                .addBannerLifecycleObserver(this)//添加生命周期观察者
//                .setIndicator(new CircleIndicator(getActivity()));
    }

    //二维码解析内容
    @Override
    public void contentSuccess(JsonMap jsonMap) {

    }

}